package com.lagou.edu.service.impl;

import com.lagou.edu.mapper.ResumeMapper;
import com.lagou.edu.pojo.Resume;
import com.lagou.edu.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
@Transactional
public class ResumeServiceImpl implements ResumeService {

    @Autowired
    private ResumeMapper resumeMapper;

    @Override
    public Resume addResume(Resume resume) {
        if (Objects.isNull(resume) || resume.getUsername().isEmpty() ||
                resume.getPassword().isEmpty()) {
            return null;
        }
        Resume resume1 = resumeMapper.findOneByUsername(resume.getUsername());
        if (Objects.nonNull(resume1)) {
            return null;
        }
        return resumeMapper.save(resume);
    }

    @Override
    public Resume findResumeByName(Resume resume) {
        if (Objects.nonNull(resume) && !resume.getUsername().isEmpty() &&
                !resume.getPassword().isEmpty()) {
            Resume resume1 = resumeMapper.findOneByUsername(resume.getUsername());
            if (resume1.getPassword().equals(resume.getPassword())) {
                return resume1;
            }
        }
        return null;
    }

    @Override
    public Resume findResumeByUsername(String username) {
        if (!username.isEmpty()) {
            return resumeMapper.findOneByUsername(username);
        }
        return null;
    }

    @Override
    public Resume login(String username, String password) {
        if (username.isEmpty() || password.isEmpty()) {
            return null;
        }
        Resume resume = resumeMapper.findOneByUsername(username);
        if (Objects.isNull(resume)) {
            return null;
        }
        if (! password.equals(resume.getPassword())) {
            return null;
        }
        return resume;
    }

}
